/**
 * 
 */
package picobot.implementation;

import java.io.*;
import java.text.StringCharacterIterator;



import picobot.interfaces.core.IMap;
import picobot.interfaces.core.IMapBuilder;

/**
 * @author stefanhauck
 *
 */
public class MapBuilder implements IMapBuilder{
	
	String [][] map;

	@Override
	public void parseMap(String stringRepresentation) {
		StringCharacterIterator sci = new StringCharacterIterator(stringRepresentation);
		int xWidth = getWidthOfMapString(sci,0);
		int yHeight = getHeightOfMapString(sci, 0);
		map = new String [xWidth][yHeight];
		convertMapStringToMapArray(xWidth, yHeight, sci, 0);
	}
	
		
	private int getWidthOfMapString(StringCharacterIterator sci, int pos){
		int width = 0;
		pos = 0;
		sci.setIndex(pos);
		if ('\n'==sci.current())
			return width;
		else 
			width++;
			getWidthOfMapString(sci, pos +1);
		return -1;
	}
	
	private int getHeightOfMapString(StringCharacterIterator sci, int pos){
		int height =1;
		pos = 0;
		sci.setIndex(pos);
		if (sci.getEndIndex()==pos)
			return height;
		else
			if ('\n'==sci.current())
				height++;
		getHeightOfMapString(sci, pos++);
		return -1;	
	}
	
	private void convertMapStringToMapArray(int xWidth, int yHeight, StringCharacterIterator sci, int iteratorPos){
		iteratorPos = 0; //Position des Iterators im Gesamtstring
		int linePos = 0; //Position des Iterators in der Zeile des Strings
		sci.setIndex(iteratorPos);
		if(linePos == xWidth && yHeight == 0){
			if(sci.current() == '#')
				placeWall(xWidth,0);
		}
		if(sci.current()=='#'){
			placeWall(linePos, yHeight);
			linePos++;
			convertMapStringToMapArray(xWidth, yHeight, sci,iteratorPos++);
		}
		if(sci.current()==' '){
			placeFreeCell(linePos, yHeight);
			linePos++;
			convertMapStringToMapArray(xWidth, yHeight, sci, iteratorPos++);
		}
		if(sci.current()=='\n'){
			linePos=0;
			convertMapStringToMapArray(xWidth, yHeight, sci, iteratorPos++);
		}
					
	}
	


//	@Override
//	public void parseMap(File mapFile) {
//		try 
//		{
//			FileInputStream file = new FileInputStream(mapFile);
//			boolean eof = false;
//			int count = 0;
//			int width = 0;
//			int height = 0;
//			boolean flag1 = true;
//			
//			while (!eof) {
//				int input = file.read();
//				if (input == 35) {if (flag1) width++;}
//				if (input == 32) {if (flag1) width++;}
//				if (input == 10) {height++; flag1 = false;}
//				if (input == -1) eof = true;
//				else count++;
//			}
//			System.out.println(height + " " + width + "\n");
//			
//			file.close();	
//			count = 0;
//			eof = false;
//			
//			while (!eof) {
//				int input = file2.read();
//				if (input == 35) {System.out.print("#");}
//				if (input == 32) {System.out.print(" ");}
//				if (input == 10) {System.out.print("\n");}
//				if (input == -1) eof = true;
//				else count++;
//				
//			}
//			file2.close();
//		}
//		catch (IOException e) {
//			System.out.println("Error -- " + e.toString());
//		}
//		
//	}

	@Override
	public void createEmptySquareMap(int size) {// fertig implementieren
		map = new String [size][size];
		int x = 0;
		int y = 0;
		if(x==0||x==size){
			
		}
		
	}

	@Override
	public void placeWall(int x, int y) {
		map [x][y] = "WALL";
		
	}

	@Override
	public void placeFreeCell(int x, int y) {
		map [x][y] = "FREE";
		
	}

	@Override
	public IMap getCurrentMap() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void modify(IMap map) {
		// TODO Auto-generated method stub
		
	}

}
